//
//  HHChatDetailViewController.m
//  HHChatKit
//
//  Created by Henry on 2021/8/19.
//

#import "HHChatDetailViewController.h"
#import <HHTool/HHTool.h>
#import "HHChatConfiguration.h"
#import "HHDetailUserCell.h"
#import "HHDetailUserModel.h"

@interface HHChatDetailViewController ()

@property (nonatomic, strong) HHDetailUserModel *userModel;

@end

@implementation HHChatDetailViewController

- (instancetype)init
{
    self = [super init];
    if (self) {
        _titleColor = [UIColor hh_colorWithLightColor:[UIColor blackColor] darkColor:[UIColor whiteColor]];
        _titleFont = [UIFont systemFontOfSize:17];
        _detailColor = [UIColor hh_colorWithLightColor:[UIColor grayColor] darkColor:[UIColor grayColor]];
        _detailFont = [UIFont systemFontOfSize:17];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [HHChatConfiguration shareInstance].backgroundColor;
    self.tableView.backgroundColor = self.view.backgroundColor;
    self.tableView.estimatedRowHeight = 0;
    self.tableView.estimatedSectionHeaderHeight = 0;
    self.tableView.estimatedSectionFooterHeight = 0;
}

- (XLFormRowDescriptor *)addRowUser:(HHDetailUserModel *)model {
    self.userModel = model;
    XLFormRowDescriptor *row = [XLFormRowDescriptor formRowDescriptorWithTag:HHFormRowDescriptorTypeDetailUser rowType:HHFormRowDescriptorTypeDetailUser];
    row.value = model;
    return row;
}

- (XLFormRowDescriptor *)rowWithTitle:(NSString *)title {
    XLFormRowDescriptor *row = [XLFormRowDescriptor formRowDescriptorWithTag:title rowType:XLFormRowDescriptorTypeButton title:title];
    [self defaultConfig:row];
    return row;
}

- (XLFormRowDescriptor *)rowSwitchWithTitle:(NSString *)title {
    XLFormRowDescriptor *row = [XLFormRowDescriptor formRowDescriptorWithTag:title rowType:XLFormRowDescriptorTypeBooleanSwitch title:title];
    [self defaultConfig:row];
    return row;
}

- (XLFormRowDescriptor *)rowButtonWithTitle:(NSString *)title color:(UIColor *)color {
    XLFormRowDescriptor *row = [XLFormRowDescriptor formRowDescriptorWithTag:title rowType:XLFormRowDescriptorTypeButton title:title];
    [row.cellConfig setObject:color forKey:@"textLabel.color"];
    row.height = 50;
    return row;
}

- (XLFormRowDescriptor *)rowArrowWithTitle:(NSString *)title viewControllerClass:(Class)viewControllerClass {
    XLFormRowDescriptor *row = [XLFormRowDescriptor formRowDescriptorWithTag:title rowType:XLFormRowDescriptorTypeButton title:title];
    [row.cellConfig setObject:@(UITableViewCellAccessoryDisclosureIndicator) forKey:@"accessoryType"];

    [self defaultConfig:row];

    if (viewControllerClass) {
        row.action.viewControllerClass = viewControllerClass;
    }
    
    return row;
}

- (XLFormRowDescriptor *)rowArrowWithTitle:(NSString *)title detail:(NSString *)detail viewControllerClass:(Class)viewControllerClass {
    XLFormRowDescriptor *row = [XLFormRowDescriptor formRowDescriptorWithTag:title rowType:XLFormRowDescriptorTypeButton title:title];
    [row.cellConfig setObject:@(UITableViewCellAccessoryDisclosureIndicator) forKey:@"accessoryType"];
    row.cellStyle = UITableViewCellStyleValue1;
    row.value = detail;
    
    [self defaultConfig:row];

    if (viewControllerClass) {
        row.action.viewControllerClass = viewControllerClass;
    }
    
    return row;
}

- (void)defaultConfig:(XLFormRowDescriptor *)row {
    row.height = 50;

    [row.cellConfig setObject:@(NSTextAlignmentNatural) forKey:@"textLabel.textAlignment"];

    // 背景
//    [row.cellConfigAtConfigure setObject:[UIColor bgCell] forKey:@"backgroundColor"];
            
    // title
    [row.cellConfig setObject:_titleColor forKey:@"textLabel.color"];
    [row.cellConfig setObject:_titleFont forKey:@"textLabel.font"];

    // detail
    [row.cellConfig setObject:_detailColor forKey:@"detailTextLabel.color"];
    [row.cellConfig setObject:_detailFont forKey:@"detailTextLabel.font"];
        
}

/// 自定义节的页眉
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return 0.01;
    }
    return 10;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.01;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return [[UIView alloc] init];
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [[UIView alloc] init];
}

/// 屏幕将要旋转
- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
    /// 可正确获取到安全区域
    [coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext>  _Nonnull context) {
        if (self.userModel) {
            if (@available(iOS 11.0, *)) {
                self.userModel.width = size.width - self.view.safeAreaInsets.left - self.view.safeAreaInsets.right;
            } else {
                self.userModel.width = size.width;
            }
        }
        [self.tableView reloadData];
    } completion:nil];
}

@end
